package Application.Util;

import Application.Config.Config;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;

public class StaticUtil
{
    protected StaticUtil() {}

    private static final Config config = Config.configReader();
    public static JFrame mainMenuFrame = new JFrame();
    public static JFrame summonerFrame = new JFrame();
    public static JFrame AI_Setter_Frame = null;
    public static JPanel AI_Panel = null;
    public static int ErrNum_Wrong_Parameter = 1;

    public static JFrame getMainMenuFrame()
    {
        return mainMenuFrame;
    }
    public static JFrame getSummonerFrame() { return summonerFrame; }
    public static int getAI_PanelHeight() { Init_AI_Panel(); return AI_Panel.getHeight(); }

    public static void Init_AI_Panel ()
    {
        AI_Panel = null;
        AI_Panel = new JPanel();
        if (config.background_file != null)
        {
            JLabel Pic_Label = new JLabel(FrameUtil.getImage());
            AI_Panel.add(Pic_Label);
        }
    }

    public static void Init_AI_SetterFrame()
    {
        if (AI_Setter_Frame == null)
        {
            AI_Setter_Frame = new JFrame();
            AI_Setter_Frame.setTitle("AI设置器");
            AI_Setter_Frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            AI_Setter_Frame.setSize(1200, 500);
            JPanel panel = new JPanel();

            JButton set_AI_Pic = new JButton("设置AI图片");
            set_AI_Pic.setFont(config.fontGetter());
            set_AI_Pic.addActionListener(e -> {
                JFileChooser chooser = new JFileChooser();
                chooser.setFont(config.fontGetter());
                chooser.setDialogTitle("选择你喜欢的AI图片吧");
                chooser.setAcceptAllFileFilterUsed(false);
                chooser.addChoosableFileFilter(new FileFilter() {
                    @Override
                    public boolean accept(File f) {
                        if (f.isDirectory()) {
                            return true;
                        } else return f.getName().endsWith(".png") || f.getName().endsWith(".jpg");
                    }

                    @Override
                    public String getDescription() {
                        return "图片文件文件(.png/.jpg)";
                    }
                });
                chooser.setFileSelectionMode(JFileChooser.OPEN_DIALOG);
                int result = chooser.showOpenDialog(mainMenuFrame);
                if (result == JFileChooser.APPROVE_OPTION)
                {
                    config.background_file = chooser.getSelectedFile();
                    Config.configWriter(config);
                }
            });

            panel.add(set_AI_Pic);

            AI_Setter_Frame.add(panel);
            AI_Setter_Frame.addWindowListener(new WindowListener() {
                @Override
                public void windowOpened(WindowEvent e) {

                }

                @Override
                public void windowClosing(WindowEvent e) {
                    AI_Setter_Frame = null;
                }

                @Override
                public void windowClosed(WindowEvent e) {

                }

                @Override
                public void windowIconified(WindowEvent e) {

                }

                @Override
                public void windowDeiconified(WindowEvent e) {

                }

                @Override
                public void windowActivated(WindowEvent e) {

                }

                @Override
                public void windowDeactivated(WindowEvent e) {

                }
            });
            AI_Setter_Frame.setVisible(true);
        }
    }
    }
